﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rizmar.RizmarDataSetTableAdapters;
using CrystalDecisions.CrystalReports.Engine;
using System.Configuration;
using CrystalDecisions.Shared;
using Microsoft.SqlServer;

namespace Rizmar
{
    public partial class RelatorioForm : Form
    {
        Form parent;
        int tipo;
        internal LancamentoForm lf;

        public RelatorioForm()
        {
            lf = new LancamentoForm();
            InitializeComponent();
        }
        public RelatorioForm(RelatorioForm1 form)
        {
            InitializeComponent();
            parent = form;
        }

        public RelatorioForm(RelatorioFormDatas form, int tp)
        {
            InitializeComponent();
            parent = form;
            tipo = tp;
        }

        public RelatorioForm(MainForm form,int tp)
        {
            InitializeComponent();
            parent = form;
            tipo = tp;
        }

        public RelatorioForm(LancamentoForm form)
        {
            InitializeComponent();
            parent = form;
        }

        public RelatorioForm(ClienteForm form)
        {
            InitializeComponent();
            parent = form;
        }


        private void RelatorioForm_Load(object sender, EventArgs e)
        {
            ReportDocument rptDoc = new ReportDocument();
            ReportDocument rptDoc1 = new ReportDocument();
            RizmarDataSet ds = new RizmarDataSet();
            RizmarDataSet.ClienteDataTable cdt = new RizmarDataSet.ClienteDataTable();
            ClienteTableAdapter cta = new ClienteTableAdapter();
            RizmarDataSet.SacadoDataTable sdt = new RizmarDataSet.SacadoDataTable();
            SacadoTableAdapter sta = new SacadoTableAdapter();
            TituloTableAdapter tta = new TituloTableAdapter();
            RizmarDataSet.TituloDataTable tdt = new RizmarDataSet.TituloDataTable();
            BorderoTableAdapter bta = new BorderoTableAdapter();
            RizmarDataSet.BorderoDataTable bdt = new RizmarDataSet.BorderoDataTable();
            BorderoTableAdapter bta1 = new BorderoTableAdapter();
            RizmarDataSet.BorderoDataTable bdt1 = new RizmarDataSet.BorderoDataTable();
            DescontoTableAdapter dta = new DescontoTableAdapter();
            RizmarDataSet.DescontoDataTable ddt = new RizmarDataSet.DescontoDataTable();

            if (parent is RelatorioForm1)
            {
                if ((parent as RelatorioForm1).chequeRadioButton.Checked )
                    if ((parent as RelatorioForm1).vencidosCheckBox.Checked)
                        tta.PreencheChequeVencidos(tdt, (parent as RelatorioForm1).dateTimePicker1.Text, (parent as RelatorioForm1).dateTimePicker2.Text);
                    else
                        tta.PreencheCheque(tdt, (parent as RelatorioForm1).dateTimePicker1.Text, (parent as RelatorioForm1).dateTimePicker2.Text);
                if ((parent as RelatorioForm1).duplicataRadioButton.Checked)
                    if ((parent as RelatorioForm1).vencidosCheckBox.Checked)
                        tta.PreencheDuplicataVencidos(tdt, (parent as RelatorioForm1).dateTimePicker1.Text, (parent as RelatorioForm1).dateTimePicker2.Text);
                    else
                        tta.PreencheDuplicata(tdt, (parent as RelatorioForm1).dateTimePicker1.Text, (parent as RelatorioForm1).dateTimePicker2.Text);
                if ((parent as RelatorioForm1).ambosRadioButton.Checked)
                    if ((parent as RelatorioForm1).vencidosCheckBox.Checked)
                        tta.PreencheAmbosVencidos(tdt, (parent as RelatorioForm1).dateTimePicker1.Text, (parent as RelatorioForm1).dateTimePicker2.Text);
                    else
                    {                        
                        tta.PreencheAmbos(tdt, (parent as RelatorioForm1).dateTimePicker1.Text, (parent as RelatorioForm1).dateTimePicker2.Text);
                    }
                ds.Tables["Titulo"].Merge(tdt);
                rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\Relatorio1.rpt");
            }
            if (parent is MainForm && tipo == 1)
            {
                cta.Fill(cdt);
                ds.Tables["Cliente"].Merge(cdt);
                rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\Relatorio2.rpt");
            }

            if (parent is MainForm && tipo == 2)
            {
                sta.Fill(sdt);
                ds.Tables["Sacado"].Merge(sdt);
                rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\Relatorio3.rpt");
            }

            if (parent is MainForm && tipo == 3)
            {
                tta.PreencheComTitulosDeHoje(tdt);
                ds.Tables["Titulo"].Merge(tdt);
                rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\Relatorio5.rpt");
            }

            /*
            if (parent is MainForm && tipo == 4)
            {
                int max_id = Convert.ToInt32(bta.SelectMaxId());
                bta.Fill(bdt, max_id);
                dta.Fill(ddt, Convert.ToInt32(bdt.Rows[0]["id_cliente"]));
                ds.Tables["Bordero"].Merge(bdt);
                ds.Tables["Desconto"].Merge(ddt);
                if ((parent as LancamentoForm).servicoRadioButton.Checked == false)
                {
                    rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\borderoReport.rpt");
                    rptDoc.OpenSubreport(@"C:\Users\Fernando\Desktop\bruno\Descontos.rpt");

                }
                else
                    rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\borderoServico.rpt");
            }
            */
            if (parent is RelatorioFormDatas && tipo == 1)
            {
                tta.PreencheBaixados(tdt, (parent as RelatorioFormDatas).dateTimePicker1.Text, (parent as RelatorioFormDatas).dateTimePicker2.Text);
                ds.Tables["Titulo"].Merge(tdt);
                rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\Relatorio4.rpt");
            }

            if (parent is RelatorioFormDatas && tipo == 2)
            {
                tta.PreencheAmbos(tdt, (parent as RelatorioFormDatas).dateTimePicker1.Text, (parent as RelatorioFormDatas).dateTimePicker2.Text);
                ds.Tables["Titulo"].Merge(tdt);
                rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\Relatorio6.rpt");
            }

            if (parent is RelatorioFormDatas && tipo == 3)
            {
                tta.PreencheAmbos(tdt, (parent as RelatorioFormDatas).dateTimePicker1.Text, (parent as RelatorioFormDatas).dateTimePicker2.Text);
                ds.Tables["Titulo"].Merge(tdt);
                rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\Relatorio7.rpt");
            }


            if (parent is ClienteForm)
            {
                tta.PreencheClienteEspecifico(tdt,Convert.ToInt32((parent as ClienteForm).idTextBox.Text));
                ds.Tables["Titulo"].Merge(tdt);
                rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\TitulosDoCliente.rpt");
            }
            if (parent is LancamentoForm)
            {
                confirmarButton.Visible = true;
                modificar.Visible = true;
                int max_id = Convert.ToInt32(bta.SelectMaxId());
                bta.Fill(bdt, max_id);
                dta.Fill(ddt, Convert.ToInt32(bdt.Rows[0]["id_cliente"]));
                ds.Tables["Bordero"].Merge(bdt);
                ds.Tables["Desconto"].Merge(ddt);
                if ((parent as LancamentoForm).servicoRadioButton.Checked == false)
                {
                    rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\borderoReport.rpt");
                    rptDoc.OpenSubreport(@"C:\Users\Fernando\Desktop\bruno\Descontos.rpt");
                    
                }
                else
                    rptDoc.Load(@"C:\Users\Fernando\Desktop\bruno\borderoServico.rpt");
            }


            //set dataset to the report viewer.
            rptDoc.SetDataSource(ds);
            //rptDoc.SetDataSource(ds1);
            crystalReportViewer1.ReportSource = rptDoc;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void modificar_Click(object sender, EventArgs e)
        {
            this.Close();
            (parent as LancamentoForm).setStatusModificando();
            (parent as LancamentoForm).salvar();
        }
    }
}
